$().ready(function() {
	validateRule();
	$.ajax({
		cache : true,
		type : "get",
		url : "/jzApi/examinations/selExaminationCom",
		data:{
		},
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {

			if (data.code == 0) {

				$('#cbatchId').selectPage({
					showField: 'name',
					keyField: 'id',
					data: data.data,
					eAjaxSuccess : function(d){
						var result;
						if(d) result = d.data;
						else result = undefined;
						return result;
					},
					eSelect : function(data){
						$('#cbatchId').val(data.id);
					}
				});

			} else {
				parent.layer.alert(data.msg)
			}
		}
	});




	//省
	$.ajax({
		cache : true,
		type : "get",
		url : "/jzweb/provincial/listProvincial",
		data:{
			pageNumber:1,
			pageSize:100
		},
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {

			if (data.code == 0) {

				$('#province').selectPage({
					showField: 'provincial',
					keyField: 'pid',
					data: data.data.records,
					eAjaxSuccess : function(d){

					},
					eSelect: function (data) {
						if(data){
							$.ajax({
								cache : true,
								type : "get",
								url : "/jzweb/provincial/listCity",
								data : {
									pid:data.pid,
									pageNumber:1,
									pageSize:100
								},// 你的formid
								async : false,
								error : function(request) {
									parent.layer.alert("Connection error");
								},
								success : function(datas) {
									if (datas.code == 0) {
										var arr = new Array();
										$.each(datas.data.records, function(i,row){
											if(row.pid == data.pid) arr.push(row);

											console.log('data',data.pid)
											console.log('row',row.pid)
											console.log('type',row.pid==data.pid)
										});
										//从城市数据中过滤省份是选择的省，并刷新城市的选择器数据
										if(arr.length){
											console.log('arr',arr)
											$('#city').selectPageData(arr)
										};
									} else {
										parent.layer.alert(data.msg)
									}

								}
							});
						}
					},
					eClear: function(){//清空时，同时清空城市内容
						$('#city').selectPageClear();
						$('#city').selectPageData([]);
					}
				});

			} else {
				parent.layer.alert(data.msg)
			}
		}
	});
	//城市选择器的初始化，使用空数据进行初始化
	// $('#city').selectPage({
	// 	showField: 'city',
	// 	keyField: 'cid',
	// 	data: []
	// });
	var  getProvinceId = $('#getProvinceId').val()
	if(getProvinceId){
		$.ajax({
			cache : true,
			type : "get",
			url : "/jzweb/provincial/listCity",
			data : {
				pid:getProvinceId,
				pageNumber:1,
				pageSize:100
			},// 你的formid
			async : false,
			error : function(request) {
				parent.layer.alert("Connection error");
			},
			success : function(datas) {
				if (datas.code == 0) {
					var arr = new Array();
					$.each(datas.data.records, function(i,row){
						if(row.pid == getProvinceId) arr.push(row);
					});
					//从城市数据中过滤省份是选择的省，并刷新城市的选择器数据
					if(arr.length){
						console.log('arr',arr)
						//$('#city').selectPageData(arr)
						$('#city').selectPage({
							showField: 'city',
							keyField: 'cid',
							data: arr
						});
						// $('#city').val($('#getCityId').val());
						// $('#city').selectPageRefresh();
					};
				} else {
					parent.layer.alert(data.msg)
				}

			}
		});
	}
	// $('#province').val($('#getProvinceId').val());
	// $('#province').selectPageRefresh();
	//
	// $('#city').val($('#getCityId').val());
	// $('#city').selectPageRefresh();

	// $('#province').on('eSelect', function() {
	// 	// 执行相应的操作...
	// 	console.log('eSelect triggered!');
	// 	alert('2323')
	// });
	//$('#province').trigger('eSelect');
	$("#sex").val($("#sexHidden").val()); //性别
	$("#sex2").val($("#sexHidden2").val()); //性别
	$("#sex3").val($("#sexHidden3").val()); //性别

	nation();

});

function nation() {
	var nations = ["汉族","蒙古族","回族","藏族","维吾尔族","苗族","彝族","壮族","布依族","朝鲜族","满族","侗族","瑶族","白族","土家族",
		"哈尼族","哈萨克族","傣族","黎族","傈僳族","佤族","畲族","高山族","拉祜族","水族","东乡族","纳西族","景颇族","柯尔克孜族",
		"土族","达斡尔族","仫佬族","羌族","布朗族","撒拉族","毛南族","仡佬族","锡伯族","阿昌族","普米族","塔吉克族","怒族", "乌孜别克族",
		"俄罗斯族","鄂温克族","德昂族","保安族","裕固族","京族","塔塔尔族","独龙族","鄂伦春族","赫哲族","门巴族","珞巴族","基诺族"];
	var val =$("#nationHidden").val();
	var val2 =$("#nationHidden2").val();
	var val3 =$("#nationHidden3").val();
	if(nations.length==0){
		$("#nation").append('<option value="">暂无数据</option>');
		$("#nation2").append('<option value="">暂无数据</option>');
		$("#nation3").append('<option value="">暂无数据</option>');
	}else{
		$("#nation").append('<option value="">请选择</option>');
		$("#nation2").append('<option value="">请选择</option>');
		$("#nation3").append('<option value="">请选择</option>');
		for(var p in nations){
			if(val!=nations[p]){
				$("#nation").append('<option value="'+nations[p]+'">'+nations[p]+'</option>');
			}else{
				$("#nation").append('<option selected  value="'+nations[p]+'">'+nations[p]+'</option>');
			}
			if(val2!=nations[p]){
				$("#nation2").append('<option value="'+nations[p]+'">'+nations[p]+'</option>');
			}else{
				$("#nation2").append('<option selected  value="'+nations[p]+'">'+nations[p]+'</option>');
			}
			if(val3!=nations[p]){
				$("#nation3").append('<option value="'+nations[p]+'">'+nations[p]+'</option>');
			}else{
				$("#nation3").append('<option selected  value="'+nations[p]+'">'+nations[p]+'</option>');
			}
		}
	}
}

$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});

function update() {
	var loadings = layer.load();
	$.ajax({
		cache : true,
		type : "POST",
		url : "/jzweb/careSignUpComReality/update",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
			layer.close(loadings);
		},
		success : function(data) {
			layer.close(loadings);
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			},
			idNumber : {
				required : true
			},
			ticketNumber : {
				required : true
			},
			// cbatchId : {
			// 	required : true
			// },
			unitName : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入姓名"
			},
			idNumber : {
				required : icon + "请输入身份证号码"
			},
			ticketNumber : {
				required : icon + "请输入联系方式"
			},
			// cbatchId : {
			// 	required : icon + "请选择所属竞赛"
			// },
			unitName : {
				required : icon + "请输入参赛单位"
			}
		}
	})
}

